package com.yxplearn.mybatisplus.collision;

import javafx.geometry.BoundingBox;

import java.awt.*;

/**
 * @Auther : yxpweb
 * @Date : 2020/12/5 21:40
 * @Description: 碰撞检测
 */
public class MyRectangleTest {

    public static void main(String[] args) {

        // 矩形与矩形的检测
        Rectangle rectangle1 = new Rectangle(0, 0, 2, 2);
        Rectangle rectangle2 = new Rectangle(2, 2, 2, 2);
        System.out.println(checkRectangle(rectangle1, rectangle2));

        // 圆与圆
//        Circle circle1 = new Circle(1,1,1);
//        Circle circle2 = new Circle(3,3,1);

        // 圆与矩形

        // 线与矩形

        // 线与圆


        // 另一种矩形判断
        BoundingBox boundingBox = new BoundingBox(0, 0, 50, 50);
        boolean contains = boundingBox.contains(new BoundingBox(50, 50, 100, 100));
        boolean intersects = boundingBox.intersects(new BoundingBox(50, 50, 100, 100));
        System.out.println(contains);
        System.out.println(intersects);
    }


    public static double getDistance(Point p1, Point p2) {
        double _x = Math.abs(p2.x - p1.x);
        double _y = Math.abs(p2.x - p1.y);
        return Math.sqrt(_x * _x + _y * _y);
    }

    public static boolean checkCircle(Circle circle1, Circle circle2) {
        return getDistance(circle1.getPoint(), circle2.getPoint()) < (circle1.getRadius() + circle2.getRadius());
    }

    /**
     * 矩形的碰撞检测
     *
     * @param r1
     * @param r2
     * @return true 有碰撞
     */
    public static boolean checkRectangle(Rectangle r1, Rectangle r2) {
        Rectangle intersection = r1.intersection(r2);
        System.out.println(intersection);
        if (intersection.width >= 0 && intersection.height >= 0) {
            return true;
        }
        return false;
    }
}
